
function AjaxFormSubmit(jDomFinder, successFunc, errorFunc, completeFunc, checkFunc) {
    var jDom = $(jDomFinder);
    jDom.submit(function(event) {
        if(checkFunc && !checkFunc()) {
            return false;
        }
        event.preventDefault();
        AjaxForm(jDomFinder, successFunc, errorFunc, completeFunc)
        return false;
    });
}

function AjaxForm(jDomFinder, successFunc, errorFunc, completeFunc) {
    var jDom = $(jDomFinder);
    var method = jDom.attr("method");
    Ajax(jDom.attr("action"), jDom.serializeArray(), successFunc, errorFunc, completeFunc, method);
}

function Ajax(url, data, successFunc, errorFunc, completeFunc, method)
{
    if(!method) {
        method = 'post';
    }
    $.ajax({
        url: url,
        type: method,
        dataType: 'json',
        data: data,
        success: function(json) {
            if(json.status == 1) {
                if(successFunc)
                    successFunc(json);
                if(json.data && json.data.redirect) {
                    setTimeout(function() {
                        if (json.data.redirect == 'reload') {
                            window.location.reload();
                        } else if(json.data.redirect == 'goback') {
                            history.go(-1);
                        } else {
                            window.location.href = json.data.redirect;
                        }
                    }, 2000);
                }
            } else {
                if(errorFunc) {
                    errorFunc(json.msg, json.data);
                    if(json.data && json.data.redirect) {
                        setTimeout(function() {
                            if (json.data.redirect == 'reload') {
                                window.location.reload();
                            } else if(json.data.redirect == 'goback') {
                                history.go(-1);
                            } else {
                                window.location.href = json.data.redirect;
                            }
                        }, 2000);
                    }
                }
            }
        },
        error: function() {
            alert("网络请求失败");
        },
        complete: function() {
            if(completeFunc)
                completeFunc();
        }
    });
}

function showInit() {
    $("#errorTips").addClass("hidden");
    $("#successTips").addClass("hidden");
}
function showError(msg) {
    $("#errorTips").removeClass("hidden");
    $("#errorMsg").html(msg);
}
function showSuccess(msg) {
    $("#successTips").removeClass("hidden");
    $("#successMsg").html(msg);
}